package bairui.international.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianjing.sh.web.security.authority.login.Util_Login;

import bairui.international.model.domain.jpa.TB_Admin;
import bairui.international.model.domain.jpa.TB_Prize;
import bairui.international.model.domain.jpa.TB_PrizeItem;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.domain.jpa.TB_WinPrize;
import bairui.international.model.domain.jpa.id.ID_WinPrize;
import bairui.international.model.type.Type_User;
import bairui.international.service.Service_PrizeItem;
import bairui.international.service.Service_User;
import bairui.international.service.Service_WinPrize;

/**
 * 
 * 【(中奖)TB_WinPrize】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_WinPrize 模型
 * @see Service_WinPrize 服务接口
 * @see BService_WinPrizeImpl 服务实现基础
 *
 */
@Service
public class Service_WinPrizeImpl extends BService_WinPrizeImpl implements Service_WinPrize {

	@Autowired private Service_User service_User;
	@Autowired private Service_PrizeItem service_PrizeItem;
	
	private TB_WinPrize createInstance(
		TB_User _user, TB_Prize prize, TB_PrizeItem _prizeItem, 
		String phone, String imgUrl
	){
		TB_PrizeItem prizeItem = service_PrizeItem.load(_prizeItem);
		TB_User user = service_User.load(_user);
		return dao.save(new TB_WinPrize(
				new TB_WinPrize.MustProperties() {
					@Override public TB_PrizeItem getPrizeItem() {
						return prizeItem;
					}
					@Override public ID_WinPrize getId() {
						return new ID_WinPrize(user, prize);
					}
					@Override public Boolean getNoPrizeFlag() {
						return prizeItem.getNoPrizeFlag();
					}
					@Override public Boolean getFinishFlag() {
						return false;
					}
				},
				new TB_WinPrize.OptionalSingleProperties() {
					@Override public String getPhone() {
						return phone;
					}
					@Override public Double getRate() {
						return prizeItem.getRate();
					}
					@Override public String getPrizeName() {
						return prizeItem.getPrizeName();
					}
					@Override public String getImgUrl() {
						return imgUrl;
					}
					@Override public String getRealName() {
						return user.getRealName();
					}
					@Override public String getNickName() {
						return user.getNickName();
					}
					@Override public Date getFinishTime() {
						return null;
					}
					@Override public TB_Admin getFinishor() {
						return null;
					}
					@Override public Type_User getUserType() {
						return user.getType();
					}
				}
			));
	}
	
	@Override
	public TB_WinPrize create(TB_User user, TB_PrizeItem prizeItem, String imgUrl) throws Exception {
		user = service_User.load(user);
		prizeItem = service_PrizeItem.load(prizeItem);
		//
		return this.createInstance(user, prizeItem.getPrize(), prizeItem, user.getPhone(), imgUrl);
	}
	
	@Override
	public void finish(ID_WinPrize winId) throws Exception {
		TB_WinPrize bean = findById(winId);
			bean.setFinishFlag(true);
			bean.setFinishTime(new Date());
			bean.setFinishor((TB_Admin)Util_Login.getInfo().getUser());
	}

}
